/**
 * Copyright 2009-2010 - YangJiandong(chunquedong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 你可以自由复制、传播本项目的下载包文件，但必须保持其完整性。
 * 我们不用对使用中的风险及由此造成的损失承担任何责任。
 * 详细情况请见《ChunMap许可协议》。

 * 想了解更多有关ChunMap的信息，请访问http://code.google.com/p/chunmap/
 */
package chunmap.app.bench;

import java.awt.AWTEvent;
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;

import chunmap.app.MapCtrl;
import chunmap.app.tools.navigate.PanTool;
import chunmap.app.tools.navigate.ZoomTool;

/**
 * 插件管理容器
 * @author chunquedong
 * 
 */
public class OperateManager{
	private final List<Tool> plugs;
	private Tool currentPlug;
	private MapCtrl mapBox;
	private CommandManager commandManager;

	public OperateManager(MapCtrl mapBox,Component component) {
		plugs = new ArrayList<Tool>();
		this.mapBox = mapBox;
		commandManager=new CommandManager();
		addPlug(new PanTool());
		addPlug(new ZoomTool());
		
		ComponentUtils.bindEvent(this, component);
	}
	

	//--------------------------------------------------------管理命令
	public void executeCommand(Command cmd){
		commandManager.execute(cmd);
	}
	
	public void undo(){commandManager.undo();}
	
	public void redo(){commandManager.redo();}

	//--------------------------------------------------------管理插件
	
	public void setAllInactive() {
		for (Tool p : plugs) {
			p.setActive(false);
		}
	}
	
	public void onEvent(AWTEvent event, EventType type) {
		
		for (Tool p : plugs) {
			if(p.isActive()){
				if(!p.actionEvent(event, type))return;
			}
		}
		
		if(currentPlug!=null){
			currentPlug.actionEvent(event, type);
		}
	}

	public void addPlug(Tool p) {
		plugs.add(p);
		p.onAdd(mapBox);
	}

	public void remove(Tool p) {
		plugs.remove(p);
		p.onRemove();
	}

	public Tool getPlug(String id) {
		for (Tool p : plugs) {
			if (p.getId() == id) {
				return p;
			}
		}
		return null;
	}

	public Tool getCurrentPlug() {
		return currentPlug;
	}


	public void setCurrentPlug(Tool currentPlug) {
		this.currentPlug = currentPlug;
		currentPlug.onAdd(mapBox);
	}
	
}
